package com.success.application.api;

import android.util.Log;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.success.application.AppContext;
import com.success.application.common.param.Params;

import cz.msebera.android.httpclient.HttpEntity;

@SuppressWarnings("WeakerAccess")
public class ApiHttpClient {

    public final static String HOST = "www.sdfyzx.cn";
//    private static String API_URL = "http://www.sdfyzx.cn/%s";
    private static String API_URL = "http://47.92.92.215:8087/%s";


    private static AppContext CONTEXT;
    private static AsyncHttpClient CLIENT;

    private ApiHttpClient() {
    }

    /**
     * 初始化网络请求，包括Cookie的初始化
     *
     * @param appContext AppContext
     */
    public static void init(AppContext appContext) {
        CONTEXT = appContext;
        CLIENT = new AsyncHttpClient();
    }

    public static void get(String partUrl, AsyncHttpResponseHandler handler) {
        CLIENT.get(getAbsoluteApiUrl(partUrl), handler);
        Log.d("GET", partUrl);
    }

    public static void get(String partUrl, Params params,
                           AsyncHttpResponseHandler handler) {
        params.generateVerification();
        CLIENT.get(getAbsoluteApiUrl(partUrl), params, handler);
        Log.d("GET ", partUrl + "#" + params);
    }

    public static void post(String partUrl, AsyncHttpResponseHandler handler) {
        CLIENT.post(getAbsoluteApiUrl(partUrl), handler);
        Log.d("POST ", partUrl);
    }

    public static void post(String partUrl, Params params,
                            AsyncHttpResponseHandler handler) {
        params.generateVerification();
        CLIENT.post(getAbsoluteApiUrl(partUrl), params, handler);
        Log.d("POST ", partUrl + "#" + params);
    }

    public static void postJson(String partUrl, Params params,
                            AsyncHttpResponseHandler handler) {
        params.generateVerification();
        params.setUseJsonStreamer(true);
        CLIENT.post(getAbsoluteApiUrl(partUrl), params, handler);
        Log.d("POST ", partUrl + "#" + params);
    }

    public static void post(String partUrl, HttpEntity entity,
                            AsyncHttpResponseHandler handler) {
        CLIENT.post(CONTEXT, getAbsoluteApiUrl(partUrl), entity, "application/json",
                handler);
    }
    public static void put(String partUrl, Params params,
                           AsyncHttpResponseHandler handler) {
        CLIENT.put(getAbsoluteApiUrl(partUrl), params, handler);
        Log.d("PUT ", partUrl + "?" + params);
    }

    private static String getAbsoluteApiUrl(String partUrl) {
        String url = partUrl;
        if (!partUrl.startsWith("http:") && !partUrl.startsWith("https:")) {
            url = String.format(API_URL, partUrl);
        }
        return url;
    }

    public static void getDirect(String url, AsyncHttpResponseHandler handler) {
        CLIENT.get(url, handler);
        Log.d("GET " , url);
    }
}
